<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Rule;
use app\index\model\Advert;
use app\index\model\Visit;

class AdvertPage extends Controller{
    //
    public function index($tname, $prefix = ''){
        $ip = ip();
        $user_agent = $this->request->header('user-agent', '');
        $url = $this->request->url(true);
        $referer = $this->request->server('http_referer', '');
        $log = ['tname'=>$tname, 'ip'=>$ip, 'agent'=>$user_agent, 'url'=>$url, 'referer'=>$referer];
        //
        $Visit = Visit::instance();
        $Rule = Rule::instance();
        $blockip = $Rule->Defend->isBlockIp($ip);
        if($blockip && $blockip['act'] == 2){//IP黑名单
            $log = array_merge($log, ['deny'=>2, 'types'=>8, 'tpl'=>'404错误页面']);
            $Visit->log($log);
            return $this->fetch('error/404', ['msg' => 'IP黑名单']);
        }
        //
        $advert = Advert::instance()->get($tname);
        if(!$advert){//页面不存在
            $log = array_merge($log, ['tpl'=>'页面不存在']);
            $Visit->log($log);
            return $this->fetch('error/404', ['msg' => '页面不存在！']);
        }
        if($advert['unvisit'] == 1){//页面禁止访问
            $log = array_merge($log, ['deny'=>2, 'types'=>9, 'tpl'=>'404错误页面']);
            $Visit->log($log);
            return $this->fetch('error/404', ['msg' => '页面禁止访问']);
        }
        //
        $key = 'o_o';
        $skip = $this->request->get($key, 0); //跳转
        $advert['ip'] = $ip;
        $param = [
            'tname'=> $advert['tname'],
            'user_agent' => $user_agent,
            'skip' => $skip,
            'deny' => $advert['deny'],
            'banbase' => $advert['banbase'],
            'banpc' => $advert['banpc'],
            'baids' => $advert['baids'],
            'ip' => $ip
        ];
        $resp = $Rule->run($param, $blockip);//普通规则
        unset($param, $blockip);
        $log['city'] = isset($resp['location']) ? implode(',', $resp['location']) : '';
        $log['isp'] = isset($resp['isp']) ? $resp['isp'] : '';
        $log['block'] = implode(',', $advert['baids']);
        unset($advert['baids']);
        $resp['fit'] = $resp['deny'] == true ? 1 : 0;
        $log['deny'] = $resp['deny'] ? 1 : 0;
        $log['types'] = $resp['types'];
        $skip ? $advert['mode'] == 0 : null;
        if($advert['mode'] == 1){
            $advert['skip'] = 0;
            if(!$resp['deny'] && $skip != 1){
                $advert['skip'] = 1;
                $advert['href'] = strrev(strpos($url, '?') === false ? $url.'?'.$key.'=1' : $url.'&'.$key.'=1');
                $resp['deny'] = true;
            }
        }
        if($advert['ismo'] == 1){
            $vcid = $Visit->sum($advert['aid']);
            $advert['jscode'] = '<script type="text/javascript" src="/static/index/js/tongji.min.js?vcid='.$vcid.'&dm='.$advert['domain'].'"></script>'.$advert['jscode'];
        }
        if($resp['deny']){
            $log['tpl'] = (isset($advert['skip']) && $advert['skip'] == 1) ? $advert['tnname'] : $advert['tbname'];
            $Visit->log($log);
            return $this->blockPage('index.html', $advert);
        }
        $log['tpl'] = $advert['tnname'];
        $skip == 1 ? null : $Visit->log($log);
        return $this->normalPage('index.html', $advert);
    }
    //
    public function page($tname, $dir, $page){
        $pathinfo = $this->request->pathinfo();
        $needle = '/'.$dir.'/';
        $file = substr($pathinfo, strpos($pathinfo, '/'.$dir.'/') + strlen($needle));
        $advert = Advert::instance()->get($tname);
        if(!$advert){
            return $this->fetch('error/404', ['msg' => '页面不存在！']);
        }
        unset($advert['baids']);
        if($file == 'index.html'){
            $this->redirect('/'.substr($pathinfo, 0, strpos($pathinfo, '/'.$dir.'/')));
        }
        if($dir == 'bk'){
            return $this->blockPage($file, $advert);
        }
        return $this->normalPage($file, $advert);
    }
    //
    private function blockPage($file, $advert){
        if($advert['tbno'] == ''){
            return $this->error('屏蔽模板未添加到后台');
        }
        $path = env('root_path').'public';
        $advert['url'] = '/'.$advert['tname'].'/bk';
        $advert['path'] = '/tpl/bk/'.$advert['tbno'];
        $file = $path.$advert['path'].'/'.$file;
        if(!file_exists($file)){
            return $this->error('模板文件未上传~~~b');
        }
        return $this->fetch($file, ['tpl' => $advert]);
    }
    //
    private function normalPage($file, $advert){
        if($advert['tnno'] == ''){
            return $this->error('正常模板未添加到后台');
        }
        $path = env('root_path').'public';
        $advert['url'] = '/'.$advert['tname'].'/nm';
        $advert['path'] = '/tpl/nm/'.$advert['tnno'];
        $file = $path.$advert['path'].'/'.$file;
        if(!file_exists($file)){
            return $this->error('模板文件未上传~~~n');
        }
        return $this->fetch($file, ['tpl' => $advert]);
    }
}